/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Biz.DoctorBO;
import Entity.Doctor;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Nghia
 */
public class Model_Doctor extends AbstractTableModel {

    private static final int DOCTORID_INDEX = 0;
    private static final int DNAME_INDEX = 1;
    private static final int ADDRESS_INDEX = 2;
    private static final int PHONENO_INDEX = 3;
    private static final int SPEC_INDEX = 4;
    protected String[] columnNames;
    protected java.util.List<Doctor> doctorList;

    public Model_Doctor() {
    }

    public Model_Doctor(String[] columnNames, java.util.List<Doctor> doctorList) {
        this.columnNames = columnNames;
        this.doctorList = doctorList;
    }

    @Override
    public String getColumnName(int i) {
        return columnNames[i];
    }

    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case DOCTORID_INDEX:
                return Integer.class;
            case DNAME_INDEX:
                return String.class;
            case ADDRESS_INDEX:
                return String.class;
            case PHONENO_INDEX:
                return Integer.class;
            case SPEC_INDEX:
                return String.class;
            default:
                return Object.class;
        }
    }

    @Override
    public void setValueAt(Object value, int row, int column) {
        Doctor e = (Doctor) doctorList.get(row);
        switch (column) {
            case 0:
                e.setDoctorID((Integer) value);
            case 1:
                e.setDName(String.valueOf(value));
            case 2:
                e.setAddress(String.valueOf(value));
            case 3:
                e.setPhoneNo((Integer) value);
            case 4:
                e.setSpecialization(String.valueOf(value));
            default:
                System.out.println("Invalid index");
        }
        fireTableCellUpdated(row, column);
    }

    @Override
    public int getRowCount() {
        return doctorList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int row, int column) {
        Doctor d = (Doctor) doctorList.get(row);
        switch (column) {
            case DOCTORID_INDEX:
                return d.getDoctorID();
            case DNAME_INDEX:
                return d.getDName();
            case ADDRESS_INDEX:
                return d.getAddress();
            case PHONENO_INDEX:
                return d.getPhoneNo();
            case SPEC_INDEX:
                return d.getSpecialization();
            default:
                return new Object();
        }


    }

    public void add(String D_Name, String Address, int PhoneNo, String Specialization) throws SQLException {
        DoctorBO db = new DoctorBO();
        db.addDoctor(D_Name, Address, PhoneNo, Specialization);
    }

    public void edit(int DoctorID, String D_Name, String Address, int PhoneNo, String Specialization) throws SQLException {
        DoctorBO db = new DoctorBO();
        db.editDoctor(DoctorID, D_Name, Address, PhoneNo, Specialization);
    }

    public void delete(int DoctorID) throws SQLException {
        DoctorBO db = new DoctorBO();
        db.deleteDoctor(DoctorID);
    }
}
